using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using System.IO.Ports;
using System.Text;

#if MINI
using SecretLabs.NETMF.Hardware.NetduinoMini;
#else
using SecretLabs.NETMF.Hardware.Netduino;
#endif
namespace codebase
{
    public class windowODB2 : UI.Window
    {
        UI.Frame selectFrame;
        UI.Frame dataFrame;

        UI.Button setActive;
        UI.Button setReset;
        UI.Button setProtocol;

        UI.Frame realTimeFrame;
        UI.CheckBox odbN;
        UI.CheckBox odbV;

        UI.Frame elseFrame;

        UI.Label lInfo;
        UI.Label lData;

        UI.Clock[] odbC;

        UI.Button setBack;

        SerialMonitor mon;

        public windowODB2(UI.WindowManager manager, SerialMonitor monitor)
        {
            Initialize(manager, monitor);
        }

        public void Initialize(UI.WindowManager manager, SerialMonitor monitor)
        {
            base.Initialize(manager, 0, 0);

            mon = monitor;

            FullScreen = true;
            CaptionText = "ODB2 Monitor";
            Effect = false;

            selectFrame = (UI.Frame)Add(UI.Window.ControlTypes.Frame, 1, 2);
            selectFrame.Width = cX(screen.Width / screen.CharacterWidth - 2);
            selectFrame.Height = cY(10);
            selectFrame.Caption = "Settings";

            dataFrame = (UI.Frame)Add(UI.Window.ControlTypes.Frame, 1, 13);
            dataFrame.Width = cX(screen.Width / screen.CharacterWidth - 2);
            dataFrame.Height = cY(26);
            dataFrame.Caption = "Real Time Data";

            realTimeFrame = (UI.Frame)Add(UI.Window.ControlTypes.Frame, 13, 4);
            realTimeFrame.Caption = "RT";
            realTimeFrame.Width = cX(6);
            realTimeFrame.Height = cY(6);

            elseFrame = (UI.Frame)Add(UI.Window.ControlTypes.Frame, 20, 4);
            elseFrame.Caption = "Other";
            elseFrame.Width = cX(12);
            elseFrame.Height = cY(6);

            setActive = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 3);
            setActive.Text = "\n  Active   \n";
            setActive.isStateButton = true;

            setReset = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 6);
            setReset.Text = "\n  Reset    \n";

            setProtocol = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 9);
            setProtocol.Text = "\n Protocol  \n";

            odbN = (UI.CheckBox)Add(UI.Window.ControlTypes.CheckBox, 13, 5);
            odbN.Text = "RPM";

            odbV = (UI.CheckBox)Add(UI.Window.ControlTypes.CheckBox, 13, 6);
            odbV.Text = "Volt";

            lInfo = (UI.Label)Add(UI.Window.ControlTypes.Label, 13, 11);
            lInfo.Text = "Response:";

            lData = (UI.Label)Add(UI.Window.ControlTypes.Label, 24, 11);
            lData.Text = "-";

            odbC = new UI.Clock[2];
            int count = 0;
            foreach (UI.Clock c in odbC)
            {
                odbC[count] = (UI.Clock)Add(UI.Window.ControlTypes.Clock, 1, 14 + count * 6);
                odbC[count].Value = 0;
                odbC[count].Width = screen.Width - odbC[count].Left * 2 - 2;
                odbC[count].refreshSize();
                odbC[count].FaceColor = ColorExtensions.RGB16(120, 180, 90);
                odbC[count].HandColor = ColorExtensions.RGB16(221, 21, 114);
                odbC[count].ForeColor = ColorExtensions.WHITE16;
                odbC[count].BackgroundColor = ColorExtensions.RGB16(240, 220, 140);

                count++;
            }

            odbC[0].Name = "RPM";
            odbC[0].Unit = "rpm";
            odbC[0].Value = 1200;
            odbC[0].Max = 2500;
            odbC[1].Name = "Volt";
            odbC[1].Unit = "V";
            odbC[1].Value = 13;
            odbC[1].Max = 20;

            setBack = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 36);
            setBack.Text = "\n Back \n";
            setBack.Released += new UI.Button.ButtonReleasedDelegate(back_Pressed);
        }


        void back_Pressed(UI.Button btn)
        {
            mon.formMain.Visible = true;

            if (mon.formMain.menuCalculator.isPressed) mon.formCalculator.Visible = true;
            if (mon.formMain.menuDebug.isPressed) mon.formDebug.Visible = true;

            Parent.Remove(this);

            Parent.Redraw();
        }
    }
}
